package com.dcbeida.fragment.homeFragment;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.MsgList;
import com.dcbeida.bean.MsgUnReadCount;
import com.dcbeida.bean.ShopSummary;
import com.dcbeida.bean.ShopSummaryCurrent;
import com.dcbeida.bean.UnReadList;
import com.dcbeida.bean.User;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.callback.JsonCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.DialogUtils;
import com.dcbeida.utils.JsonGet;

import java.lang.reflect.Type;
import java.util.List;

import dc.medicine.com.okhttplib.OkHttpUtils;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class HomePresenter implements HomeContract.Present {


    private HomeContract.View view;

    private ModelUtils model;

    private Activity act;

    public HomePresenter(HomeContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        model = ModelUtils.newInstance();
        this.act = act;

    }

    @Override
    public void bindDevicetoken(String devicetoken) {
        model.getDate("binddevicetoken", new JsonGet().putopt("devicetoken", devicetoken)
                .putopt("devicemodule", MyApplication.DUID)
                .putopt("devicetype", 2)
                .putopt("uid", MyApplication.user.getUid())
                .putopt("shopid", MyApplication.user.getShopid())
                .tostring(), new bindCallBack(act, User.class, lis));
    }

    @Override
    public void getShopSummaryCurrent() {
        //当月统计接口
        model.getDate("getshopsummarycurrent", new JsonGet()
                .putopt("uid", MyApplication.user.getUid())
                .putopt("shopid", MyApplication.user.getShopid())
                .tostring(), new ShopSummaryCurrentCallBack(act, ShopSummaryCurrent.class, lis));
    }

    @Override
    public void getShopSummary(String begindate, String enddate, String reporttype) {
        //当月统计接口
        model.getDate("getshopsummary", new JsonGet()
                .putopt("uid", MyApplication.user.getUid())
                .putopt("shopid", MyApplication.user.getShopid())
                .putopt("begindate", begindate)
                .putopt("enddate", enddate)
                .putopt("reporttype", reporttype)
                .tostring(), new ShopSummaryCallBack(act, ShopSummary.class, lis));
    }

    @Override
    public void getNews(String pageindex, String pagesize) {
        //当月统计接口
        model.getDate("getNotifyList", new JsonGet()
                .putopt("uid", MyApplication.user.getUid())
                .putopt("shopid", MyApplication.user.getShopid())
                .putopt("pageindex", pageindex)
                .putopt("pagesize", pagesize)
                .tostring(), new GetNewsCallBack(act, MsgList.class, lis));
    }

    @Override
    public void getdruglist() {
        model.getDate("getdruglist", new JsonGet()
                        .putopt("uid", MyApplication.user.getUid())
                        .putopt("start", 1)
                        .putopt("records", -1).tostring(), new JsonCallback<DrugListBean>(DrugListBean.class) {
                    @Override
                    public void onResponse(DrugListBean data) {
                        if (data == null)
                            return;

                        view.showDrugIds(data);

                    }

                    @Override
                    public void showError(final String message,int code,String data) {
                        Toast.makeText(OkHttpUtils.getContext(), message, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(call, response, e);
                        view.showTip("获取药品种类失败,请重新尝试");
                    }
                }
        );
    }

    @Override
    public void checklogintoken() {
        model.getDate("checklogintoken", new JsonGet()
                        .putopt("uid", MyApplication.user.getUid())
                        .putopt("token", MyApplication.user.getToken()).tostring(),
                new JsonCallback<Object>(Object.class) {
                    @Override
                    public void onResponse(Object data) {

                    }

                    @Override
                    public void showError(final String message,int code,String data) {
                        if(code ==19){
                            //token 过期
                           view.logout();
                        }else if(code ==100){
                           view.logoutTips();
                        } else
                            Toast.makeText(OkHttpUtils.getContext(), message, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(call, response, e);

                    }
                }
        );
    }

    @Override
    public void getUnReadNews() {
        //是否有未读
        model.getDate("getNotifyUnReadCount", new JsonGet()
                .putopt("uid", MyApplication.user.getUid())
                .tostring(), new UnReadNewsCallBack(act, UnReadList.class, lis));
    }

    @Override
    public void start() {

    }


    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    final class bindCallBack<T> extends DialogCallback<T> {

        public bindCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                //绑定成功失败返回
            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            //view.showTip("网络异常，请重试");
            Log.i("sean", "+++++++绑定Umeng账号失败");
        }
    }

    final class ShopSummaryCurrentCallBack<T> extends DialogCallback<T> {

        public ShopSummaryCurrentCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                view.showShopSummaryCurrent((ShopSummaryCurrent) data);
            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("网络异常，请重试");
        }
    }


    final class ShopSummaryCallBack<T> extends DialogCallback<T> {

        public ShopSummaryCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                view.showShopSummary((ShopSummary) data);
            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("网络异常，请重试");
        }
    }


    final class GetNewsCallBack<T> extends DialogCallback<T> {

        public GetNewsCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener,false);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if(data instanceof MsgList) {
                    view.showNews(((MsgList) data).getList());
                }

            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("网络异常，请重试");
        }
    }

    final class UnReadNewsCallBack<T> extends DialogCallback<T> {

        public UnReadNewsCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener,false);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if(data instanceof UnReadList){
                    MsgUnReadCount msgUnReadCount = ((UnReadList) data).get(0);
                    if(msgUnReadCount != null){
                        if(msgUnReadCount.getUnReadCountNotify()>0){
                            view.handleMsg(msgUnReadCount.getUnReadCountNotify());
                            return;
                        }
                    }
                }
            }
            view.handleMsg(0);


        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("网络异常，请重试");
        }
    }


}
